package com.cqust.proxy;

import com.cqust.aspect.MyAspect;
import com.cqust.dao.UserDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * @author 张成
 * @version 1.0
 * @date 2022/3/29 19:34
 */
public class MyProxy  implements InvocationHandler {
    UserDao userDao;

    public Object create(UserDao userDao){
        this.userDao = userDao;

        ClassLoader classLoader = MyProxy.class.getClassLoader();
        Class[] classes = userDao.getClass().getInterfaces();

        return Proxy.newProxyInstance(classLoader,classes,this);

    }



    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        MyAspect myAspect = new MyAspect();
        myAspect.log(method.getName());


        return method.invoke(userDao,args);
    }
}
